import path from "path";
import {pluginsList} from "./config";
import {type ConfigEnv, loadEnv, type UserConfigExport} from "vite";
import progress from "vite-plugin-progress";

export default ({mode}: ConfigEnv): UserConfigExport => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: env.VITE_APP_CONTEXT_PATH,
    // 路径别名
    resolve: {
      alias: {
        "@assets": path.join(__dirname, "src/assets"),
        "@": path.resolve(__dirname, "src")
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${path.resolve("src/assets/styles/variables.less")}";`
          },
          // https://lesscss.org/usage/#less-options
          math: "parens-division",
          javascriptEnabled: true
        }
      }
    },
    plugins: [pluginsList(), progress()],
    // 依赖预构建-> https://cn.vitejs.dev/config/dep-optimization-options#dep-optimization-options
    optimizeDeps: {
      include: ["mitt",
        "dayjs",
        "axios",
        "pinia",
        "@vueuse/core",
        'diagram-js/lib/navigation/movecanvas',
        'bpmn-js/lib/Viewer',
        'bpmn-js/lib/Modeler.js',
        'bpmn-js-properties-panel',
        'diagram-js/lib/navigation/zoomscroll',
        'bpmn-js/lib/features/palette/PaletteProvider',
        'bpmn-js/lib/features/context-pad/ContextPadProvider',
        'diagram-js/lib/draw/BaseRenderer',
        'tiny-svg',],
      exclude: ["@iconify-icons/lets-icons"]
    },
    server: {
      // 监听所有地址
      host: "0.0.0.0",
      // 服务启动时是否自动打开浏览器
      open: true,
      // 端口号（这里使用了变量 VITE_APP_PORT）
      port: Number(env.VITE_APP_PORT),
      // 本地跨域代理-> 代理到服务器的接口地址
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: "http://10.0.76.2:8080",
          changeOrigin: true,
          ws: true,
          rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
        }
      }
    },
    build: {
      // 浏览器兼容目标
      target: "es2015",
      // 是否生成 source map 文件
      sourcemap: false,
      rollupOptions: {
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
          compact: true,
          // 自定义 chunk
          manualChunks: {
            arco: ["@arco-design/web-vue"],
            formCreate: ["@form-create/arco-design"],
            chart: ["vue-echarts"],
            bpmnJs: ["bpmn-js"],
            vueDragGable: ["vuedraggable"],
            arcoDesignDesigner: ["arco-design-designer"],
            tinymce: ['tinymce'],
            tinymceVue: ["@tinymce/tinymce-vue"],
            vue: ["vue", "vue-router", "pinia", "@vueuse/core", "vue-i18n"]
          }
        }
      },
      minify: "terser",
      terserOptions: {
        compress: {
          keep_infinity: true, // 防止 Infinity 被压缩成 1/0，这可能会导致 Chrome 上的性能问题
          drop_console: true, // 生产环境去除 console
          drop_debugger: true // 生产环境去除 debugger
        },
        format: {
          comments: false // 删除注释
        }
      },
      // chunk 大小警告的限制
      chunkSizeWarningLimit: 2000
    }
  };
};
